function refine_nei_green(tri,v_eg,eg1,eg2,eg)
% function refine_nei_green(tri,v_eg,eg1,eg2,eg)
% we original use v_eg as the global index of vertice opsite to the longest eg
% e1---v2v3   e2---v1v3    e3---v1v2
% tri --- the golobal index of the element wait to be refined
% eg ---- the global index of the edge has been divided
% eg1, eg2 ----two new edge divided form eg, global index, too.
% v_eg --- the golbal index of new added vertices, the middle of eg
global V T E posV posT posE TE ET;
long_idx = find_long(V(T(tri,1:3),1:2));
% here we get the longest side in long_idx
% the decide the split scheme for tri according to eg and longest_idx
% refine tri first:
% always regard edge v2v3 is the longest in logical
v2_loc = mod(long_idx,3)+1;
v3_loc = mod(mod(long_idx,3)+1,3)+1;
% global index of vertices in logical turn
v1 = T(tri,long_idx);
v2 = T(tri,v2_loc);
v3 = T(tri,v3_loc);
V_long = (V(v2,:)+V(v3,:))/2;
v_long = 0; % initialize the V_long's global index
eg_loc = find(TE(tri,:)==eg);
v_eg_op = T(tri,eg_loc);
%  here we need to update the information of:
%  T,E,posT, posE, TE,ET, dofs
if v_eg_op == v1 % the side eg is the longest side
    v_long = v_eg; % they are equal!
    % add new element to T, which split tri to two elements
    tri1 = posT + 1; tri2 = posT + 2;
    T(tri2,:) = [v1,v2,v_eg];
    T(tri1,:) = [v1,v_eg,v3];
    posT = posT + 2;
    % add two new Edges to E , node global index from low to high
    eg3 = posE + 1; % eg1 and eg2 have been added by previous step
    E(eg3,:) = [v1,v_eg];
    posE = posE + 1;
    % update matrix TE,TV,EV, actually ,TV and EV are not use now,
    TE(tri1,:) = [eg1,TE(tri,v2_loc),eg3];
    TE(tri2,:) = [eg2,eg3,TE(tri,v3_loc)];
    % only set the father of new edges 1 and 2, because the mother can be set in
    % refine_nei_green, new edge 3 can be full information
    ET(eg1,2) = tri1; % the mother of old triangle
    ET(eg2,2) = tri2;
    ET(eg3,:) = [tri1,tri2];
    % Important: the information of nei 2 and nei 3
    nei2_eg = TE(tri,v2_loc);
    tri_pos = find((ET(nei2_eg,:)-tri)==0); % 1 or 2
    ET(nei2_eg,tri_pos) = tri1; % change tri to tri1
    nei3_eg = TE(tri,v3_loc);
    tri_pos = find((ET(nei3_eg,:)-tri)==0); % 1 or 2
    ET(nei3_eg,tri_pos) = tri2; % change tri to tri2    
    % kill old edge and element
    T_more(tri,2:3) = [0,1]; 
    
elseif v_eg_op == v2  
    posV = posV + 1;
    V(posV,:) = V_long;
    v_long = posV; % the new added vertices' global index
    % splint tri into three elements 
    tri1 = posT + 1; tri2 = posT + 2; tri3 = posT + 3;
    T(tri1,:) = [v1,v_long,v_eg]; 
    T(tri2,:) = [v3,v_eg,v_long];
    T(tri3,:) = [v1,v2,v_long]; 
    posT = posT + 3;
    % Add 4 edges named eg3~eg6
    eg3 = posE + 1; eg4 = posE + 2; eg5 = posE + 3; eg6 = posE + 4;
    E(eg3,:) = [v_eg,v_long];
    E(eg4,:) = [v1,v_long]; 
    E(eg5,:) = [v2,v_long];
    E(eg6,:) = [v3,v_long];
    posE = posE + 4;
    % update information in TE and ET
    TE(tri1,:) = [eg3,eg1,eg4];
    TE(tri2,:) = [eg3,eg6,eg2];
    TE(tri3,:) = [eg5,eg4,TE(tri,v3_loc)];
    ET(eg1,2) = tri1; % the mother of old triangle
    ET(eg2,2) = tri2;
    ET(eg3,:) = [tri1,tri2];
    ET(eg4,:) = [tri1,tri3];
    ET(eg5,1) = tri3; % the father of new edge 5 and 6
    ET(eg6,1) = tri2;
    % Important: the information of nei 3
    nei3_eg = TE(tri,v3_loc);
    tri_pos = find((ET(nei3_eg,:)-tri)==0); % 1 or 2
    ET(nei3_eg,tri_pos) = tri3; % change tri to tri2    
    % kill old edge and element
    E_more(TE(tri,long_idx),2:3)=[0,1];
    T_more(tri,2:3) = [0,1];
    
elseif v_eg_op == v3 
    posV = posV + 1;
    V(posV,:) = V_long;
    v_long = posV; % the new added vertices' global index
    % splint tri into three elements 
    tri1 = posT + 1; tri2 = posT + 2; tri3 = posT + 3;
    T(tri1,:) = [v2,v_long,v_eg]; 
    T(tri2,:) = [v1,v_eg,v_long];
    T(tri3,:) = [v1,v_long,v3]; 
    posT = posT + 3;
    % Add 4 edges named eg3~eg6
    eg3 = posE + 1; eg4 = posE + 2; eg5 = posE + 3; eg6 = posE + 4;
    E(eg3,:) = [v_eg,v_long]; 
    E(eg4,:) = [v1,v_long]; 
    E(eg5,:) = [v2,v_long]; 
    E(eg6,:) = [v3,v_long];
    posE = posE + 4;
    % update information in TE and ET
    TE(tri1,:) = [eg3,eg1,eg5];
    TE(tri2,:) = [eg3,eg4,eg2];
    TE(tri3,:) = [eg6,TE(tri,v2_loc),eg4];
    ET(eg1,2) = tri1; % the mother of old triangle
    ET(eg2,2) = tri2;
    ET(eg3,:) = [tri1,tri2];
    ET(eg4,:) = [tri2,tri3];
    ET(eg5,1) = tri1; % the father of new edge 5 and 6
    ET(eg6,1) = tri3;
    % Important: the information of nei 2
    nei2_eg = TE(tri,v2_loc);
    tri_pos = find((ET(nei2_eg,:)-tri)==0); % 1 or 2
    ET(nei2_eg,tri_pos) = tri3; % change tri to tri1    
    % kill old edge and element
    E_more(TE(tri,long_idx),2:3)=[0,1];
    T_more(tri,2:3) = [0,1];
else
    error('the wrong case in refine_nei_green.m');
end
% plot_t(V,T);drawnow;
% refine it's neighbour is necessary:
% find the neighbour triangle share the long with tri
nei = 0;
nei_eg = TE(tri,long_idx); % the longest is divided
if v_eg_op ~= v1 % not the first case ,so the mesh have been refined
    if ET(nei_eg,1) == tri;
        nei = ET(nei_eg,2);
    else
        nei = ET(nei_eg,1);
    end      
    if nei~=0 & T_more(nei,2) == 1% there do exist a active neighbour
       refine_nei_green(nei,v_long,eg5,eg6,nei_eg,degree);
    end  
end